//
//  ZYCDutyBulletionRecordTableDetailCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/27.
//

#import "ZYCDutyBulletionRecordTableDetailCell.h"
#import "ZYCDutyBulletionRecordDetailModel.h"
#import <UIImageView+WebCache.h>
#import <YBImageBrowser.h>

@interface ZYCDutyBulletionRecordTableDetailCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contenLabel;
@property (nonatomic, strong) NSMutableArray *imageArray;
@end

@implementation ZYCDutyBulletionRecordTableDetailCell

- (void)setModel:(ZYCBusRecordReportsModel *)model{
    _model = model;
    _titleLabel.text = model.quotaName;
    _contenLabel.text = model.appContent;
    for (UIImageView *imageView in self.imageArray) {
        [imageView removeFromSuperview];
    }
    if (model.busFileList.count > 0) {
        for (NSInteger i = 0; i < model.busFileList.count; i++) {
            ZYCBusFileListModel *m = model.busFileList[i];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.userInteractionEnabled = YES;
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.tag = i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap:)];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],m.filePath]];
            [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_image_icon"]];
            [imageView addGestureRecognizer:tap];
            [self.contentView addSubview:imageView];
            [self.imageArray addObject:imageView];

            if (i == model.busFileList.count - 1) {
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.contenLabel.mas_bottom).offset((i+1) *15.0f  + i * 183.f);
                    make.left.right.equalTo(self.contentView);
                    make.height.mas_equalTo(183.0f);
                    make.bottom.equalTo(self.contentView).offset(-17.0f);
                }];
            }else{
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.contenLabel.mas_bottom).offset((i+1) *15.0f  + i * 183.f);
                    make.left.right.equalTo(self.contentView);
                    make.height.mas_equalTo(183.0f);
                }];
            }
        }

        [self.contenLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
    }else{
        [self.contenLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
            make.bottom.equalTo(self.contentView).offset(-17.0f);
        }];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.backgroundColor = randomColor;
        self.imageArray = [NSMutableArray arrayWithCapacity:0];
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contenLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.top.equalTo(self.contentView).offset(20.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
        
        [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
    }
    return self;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kSystemBoldFont(17.0f) titleColor:HEX_RGB(@"#262A28") textAlignment:ZZTextAlignmentLeft];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)contenLabel {
    if (!_contenLabel) {
        _contenLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 14.0f) titleColor:HEX_RGB(@"#888888") textAlignment:ZZTextAlignmentLeft];
        _contenLabel.numberOfLines = 0;
    }
    return _contenLabel;
}

- (void)clickTap:(UIGestureRecognizer *)tap{
    UIImageView* imageView = (UIImageView*)tap.view;
        NSMutableArray *browserDataArr = [NSMutableArray array];
        NSArray <ZYCBusFileListModel *>*filesArray = self.model.busFileList;
        NSUInteger index = imageView.tag;
        [filesArray enumerateObjectsUsingBlock:^(ZYCBusFileListModel * _Nonnull fileModel, NSUInteger idx, BOOL * _Nonnull stop) {
            YBIBImageData *data = [YBIBImageData new];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],fileModel.filePath]];
            data.imageURL = url;
            [browserDataArr addObject:data];
        }];
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = browserDataArr;
        browser.currentPage = index;
        [browser show];
}

@end
